<?php

class Materials_Model_Comment_Storage_Table extends Workset_Model_Object_Storage_Table_Abstract
{

	protected $_objectClass 		= 'Materials_Model_Comment';

	protected $_tableClass 			= 'Materials_Table_Comment';

	protected $_iteratorClass 		= 'Workset_Model_Object_Iterator';

    protected $_materialObjectClass = 'Materials_Model_Material';

	protected $_materialTableClass = 'Materials_Table_Material';

    function save(Workset_Model_Object_Interface $object, $data = null) {
        if (!$object->getId()) {
            if (null !== $data && isset($data['created'])) {
                $date = (int)$data['created'];
            } else {
                $date = (int)$object['created'];
            }

            if ($date <= 0) {
                $date = time();
            }

            $object['created'] = $date;
        }

        return parent::save($object, $data);
    }

    function getLastComment($where = null) {

        $select = $this->_getTable()->prepareSelect($where);

        $select->from(
            $this->_getTable()->info(Zend_Db_Table_Abstract::NAME),
            array('max_id' => new Zend_Db_Expr('max(id)'))
        );

        $where = array(
            'id = ('.$select->assemble().')'
        );

        $row = $this->_getTable()->fetchRow($where);

        if (null !== $row) {
            return new $this->_objectClass($row, array(
				'storage' => $this
			));
        }

        return null;
    }

    function getPopularByMaterial($where = null, $order = null, $limit = null, $offset = null, $offsetByPage = false) {

        if (true == $offsetByPage) {
            $offset = $this->getPageOffset($limit, $offset);
        }

        $select = $this->_getTable()->select();

        $select->from(
            array('c' => $this->_getTable()->info(Zend_Db_Table_Abstract::NAME))
        );
        
        $select->join(
            array('m' => $this->_getTable($this->_materialTableClass)->info(Zend_Db_Table_Abstract::NAME)),
            'c.id = m.last_comment_id',
            array()
        );

        $select->where('m.last_comment_id is not null');

        $select->order('m.rating DESC');
        $select->order('m.id DESC');
        
        $select->limit($limit, $offset);

        if (null !== $where) {
            $where = (array)$where;

            $where = array_map(function($value) {
                 return 'c.' . $value;
            }, $where);

            foreach ($where as $key => $value) {
                if (isInt($key)) {
                    $select->where($value);
                } else {
                    $select->where($key, $value);
                }
            }
        }

        if (null !== $order) {
            $order = (array)$order;

            $order = array_map(function($value) {
                 return 'm.' . $value;
            }, $order);

            $select->order($order);
        }

        //echo $select->assemble();

        return $this->getAll($select);

    }
    
}